//
//  KLCateView.m
//  used
//
//  Created by xalo on 16/7/13.
//  Copyright © 2016年 王凯. All rights reserved.
//

#import "KLCateView.h"

@interface KLCateView ()

@property (weak, nonatomic) UIScrollView *scrollView;
/** 按钮模型数组 */
@property (nonatomic, strong)  NSArray *cateBtns;


@end

@implementation KLCateView




- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [RequestManager fetchDataWithUrl:kCateList parameter:nil method:@"GET" compeleted:^(NSData *data) {
            
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            self.cateBtns = [KLCataList mj_objectArrayWithKeyValuesArray:dict[@"result"]];
        } updateUI:^{
             [self createCateBtnsWithArray:self.cateBtns];
        }];
        
    }
    return self;
}


// 创建所有按钮
- (void)createCateBtnsWithArray:(NSArray *)cateBtns {
    
    // 一行最多5
    int maxCols = 3;
    
    // 宽度和高度
    CGFloat buttonW = self.width / maxCols;
    CGFloat buttonH = buttonW;
    
    for (int i = 0; i<cateBtns.count; i++) {
        
  
        // 创建按钮
        KLButton *button = [KLButton buttonWithType:UIButtonTypeCustom];
   
        // 监听点击
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        // 传递模型
        button.listModel = cateBtns[i];

        if ([button.listModel.hotcategoryimgurl isEqualToString:@""]) continue;
        
            [self addSubview:button];
            // 计算frame
            int col = i % maxCols;
            int row = i / maxCols;
            
            button.x = col * buttonW;
            button.y = row * buttonH;
            button.width = buttonW;
            button.height = buttonH;


    }
    
}


// 按钮点击事件
- (void)buttonClick:(KLButton *)button {
    
    if (![button.listModel.hotcategoryimgurl hasPrefix:@"http"]) return;
    
//    XMGWebViewController *web = [[XMGWebViewController alloc] init];
//    web.url = button.square.url;
//    web.title = button.square.name;
//    
//    // 取出当前的导航控制器
//    UITabBarController *tabBarVc = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
//    UINavigationController *nav = (UINavigationController *)tabBarVc.selectedViewController;
//    [nav pushViewController:web animated:YES];
}

- (void)hidden {
    
    [UIView animateWithDuration:0.5 animations:^{
//        CGAffineTransform transform = CGAffineTransformIdentity;
        self.transform = CGAffineTransformTranslate( self.transform,  kWindow.center.x - 34, -kWindow.center.x);
        self.transform = CGAffineTransformScale(self.transform, 0.01, 0.01);
        
    } completion:^(BOOL finished) {
        self.hidden = YES;
    }];

}

- (void)show {
    
    self.hidden = NO;
    [UIView animateWithDuration:0.5 animations:^{
        
        self.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
     
    }];
}


@end
